package com.gopay.common.cipher.web;

import com.gopay.common.cipher.utils.ImageSensitiveUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 获取图片base 类
 * @author ddh02
 * @ImageBaseController
 * @2017/10/19 9:33  www.gopay.com.cn Inc.All rights reserved.
 */


public class ImageBaseController {

    protected Logger logger ;

    public ImageBaseController() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    protected File cert(HttpServletRequest request,
                        HttpServletResponse response,
                        String date,
                        String custId,
                        String fileName) {
        logger.info("请求参数date={}, custId={}, fileName={}", new String[]{date, custId, fileName});

        if (StringUtils.isBlank(date) || StringUtils.isBlank(custId) || StringUtils.isBlank(fileName)) {
            logger.error("路径有误");
            return null;
        }

        String fileUrl = request.getRequestURI();

        String realPath = request.getSession().getServletContext().getRealPath(fileUrl);

        byte[] bytes ;

        try {
            //取文件
            File srcFile =  new File(realPath);

            //解密文件
            bytes = ImageSensitiveUtils.decrypt(srcFile);

            printOut(bytes, response);

        } catch (Exception e) {
            logger.info("", e);
        }

        return null;
    }


    protected File cloudCert(HttpServletRequest request,
                             HttpServletResponse response,
                             String date,
                             String email,
                             String fileName) {
        logger.info("请求参数date={}, custId={}, fileName={}", new String[]{date, email, fileName});

        if (StringUtils.isBlank(date) || StringUtils.isBlank(email) || StringUtils.isBlank(fileName)) {
            logger.error("路径有误");
            return null;
        }

        String fileUrl = request.getRequestURI();

        String realPath = request.getSession().getServletContext().getRealPath(fileUrl);

        byte[] bytes ;

        try {
            //取文件
            File srcFile =  new File(realPath);

            //解密文件
            bytes = ImageSensitiveUtils.decrypt(srcFile);

            printOut(bytes, response);

        } catch (Exception e) {
            logger.info("", e);
        }

        return null;

    }


    protected File aggregatepaymentCert(HttpServletRequest request,
                             HttpServletResponse response,
                             String date,
                             String email,
                             String fileName) {
        logger.info("请求参数date={}, custId={}, fileName={}", new String[]{date, email, fileName});

        if (StringUtils.isBlank(date) || StringUtils.isBlank(email) || StringUtils.isBlank(fileName)) {
            logger.error("路径有误");
            return null;
        }

        String fileUrl = request.getRequestURI();

        String contextPath = request.getContextPath();
        fileUrl = fileUrl.substring(contextPath.length());
        String requestURL = request.getRequestURL().toString();

        String realPath = request.getSession().getServletContext().getRealPath(fileUrl);

        byte[] bytes ;

        try {
            //取文件
            File srcFile =  new File(realPath);

            //解密文件
            bytes = ImageSensitiveUtils.decrypt(srcFile);

            printOut(bytes, response);

        } catch (Exception e) {
            logger.info("", e);
        }

        return null;

    }


    protected void printOut(byte[] bytes, HttpServletResponse response) throws IOException {
        ServletOutputStream op = null;
        try {
            if (bytes != null) {
                op = response.getOutputStream();
                op.write(bytes, 0, bytes.length);
                op.flush();
                response.flushBuffer();
            } else {
                logger.error("获取文件字节数为空... ");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            if (op != null) {
                op.close();
                op = null;
            }
        }

    }



}
